<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="68px" size="mini">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" clearable placeholder="请输入"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="福利类型" prop="walfareId">
            <el-select v-model="queryParams.walfareId" clearable placeholder="请选择" @keyup.enter.native="handleQuery">
              <el-option v-for="item in welfareOptions" :key="item.walfareId" :label="item.walfareName"
                         :value="item.walfareId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="发放时间">
            <el-date-picker v-model="dateRange" end-placeholder="结束日期" range-separator="-" start-placeholder="开始日期"
                            type="daterange" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['cld:CldWelfareSend:add']" icon="el-icon-plus" plain size="mini" type="primary"
                   @click="handleAdd">福利发放
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="CldWelfareSendList" border>
      <el-table-column align="center" label="发放时间" prop="sendTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="项目名称" prop="projectName"/>
      <el-table-column align="center" label="福利类型" prop="walfareName"/>
      <el-table-column align="center" label="发放人数" prop="totalNumber"/>
      <el-table-column align="center" label="发放总金额" prop="totalMoney">
        <template slot-scope="scope">
          <span>{{ toMoney(scope.row.totalMoney) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-hasPermi="['cld:CldWelfareSend:query']" size="mini" type="text"
                     @click="handleView(scope.row)">查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>

    <!-- 福利发放详情 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="50%">
      <el-table :data="staffList" border>
        <el-table-column align="center" label="职务" prop="projectDuitsName">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.cld_project_duits" :value="scope.row.projectDuitsName"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="岗位" prop="projectPostName">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.cld_project_post" :value="scope.row.projectPostName"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="姓名" prop="staffName"/>
        <el-table-column align="center" label="福利类型" prop="walfareName"/>
        <el-table-column align="center" label="福利金额" prop="sendMoney">
          <template slot-scope="scope">
            <span>{{ toMoney(scope.row.sendMoney) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
          <template slot-scope="scope">
            <el-button v-hasPermi="['cld:CldWelfareSend:edit']" size="mini" type="text"
                       @click="handleUpdate(scope.row)">修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>


    <!-- 福利发放新增 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="addOpen" append-to-body title="福利发放" width="40%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="福利类型" prop="walfareId">
              <el-select v-model="form.walfareId" placeholder="请选择" @change="handleWalfareChange">
                <el-option v-for="item in welfareOptions" :key="item.walfareId" :label="item.walfareName"
                           :value="item.walfareId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="福利金额" prop="money">
              <el-input v-model="form.money" :disabled="true" placeholder="请输入">
                <template slot="append">元/人</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <!-- <el-col :span="12">
            <el-form-item label="发放时间" prop="sendTime">
              <el-date-picker clearable v-model="form.sendTime" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择发放时间">
              </el-date-picker>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="发放项目">
              <el-radio-group v-model="form.projectType">
                <el-radio :label="1">全部项目</el-radio>
                <el-radio :label="2">单选项目</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.projectType == 2" :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button icon="el-icon-plus" plain size="mini" type="primary" @click="handleAddProject">选择项目
            </el-button>
          </el-col>
        </el-row>
        <el-row v-if="form.projectType == 2">
          <el-table :data="form.projectList" border>
            <el-table-column align="center" label="项目区域" prop="projectRegion">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.cld_project_region" :value="scope.row.projectRegion"/>
              </template>
            </el-table-column>
            <el-table-column align="center" label="项目名称" prop="projectName"/>
            <el-table-column align="center" label="项目人数" prop="staffNumber"/>
            <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
              <template slot-scope="scope">
                <el-button size="mini" type="text" @click="handleDeleteProject(scope.row)">移除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="addOpen = false">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 选择项目 -->
    <el-dialog :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="isProjectOpen" append-to-body title="选择项目"
               width="40%">
      <selected-project :notProjectIds="notProjectIds" @multipleInput="handleMultipleProject"></selected-project>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSelectedProject">确 定</el-button>
        <el-button @click="cancelOpenProject">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addCldWelfareSend, getCldWelfareSendDetails, listCldWelfareSend, updateDetails} from "@/api/cld/CldWelfareSend";
import {getListWelfare} from '@/api/cld/welfare'
import selectedProject from "../project/selectedProject.vue";

export default {
  name: "CldWelfareSend",
  components: {selectedProject},
  dicts: ['cld_project_region', 'cld_project_post', 'cld_project_duits'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 时间区间
      dateRange: [],
      // 总条数
      total: 0,
      // 福利管理表格数据
      CldWelfareSendList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 福利发放
      addOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        walfareId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        walfareId: [
          {required: true, message: "请选择福利类型", trigger: "change"}
        ],
        sendTime: [
          {required: true, message: "发放时间不能为空", trigger: "blur"}
        ]
      },
      // 福利津贴列表
      welfareOptions: [],
      // 选择项目弹框
      isProjectOpen: false,
      // 已选择项目
      notProjectIds: [],
      // 临时存放选择项目
      temporaryList: [],
      // 福利发放明细
      staffList: []
    };
  },
  created() {
    this.getList();
    // 查询福利津贴
    this.handleGetListWelfare();
  },
  methods: {
    /** 查询福利管理列表 */
    getList() {
      this.loading = true;
      listCldWelfareSend(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.CldWelfareSendList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询福利津贴
    handleGetListWelfare() {
      getListWelfare().then(res => {
        this.welfareOptions = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        welfareSendId: null,
        walfareId: null,
        sendTime: null,
        projectId: null,
        projectType: 1,
        projectList: []
      };
      this.resetForm("form");
    },
    // 福利类型选择
    handleWalfareChange(e) {
      let obj = this.welfareOptions.find(i => {
        return i.walfareId = e;
      })
      if (obj) {
        this.form.money = obj.walfareMoney;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.addOpen = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$prompt('', '修改金额', {
        center: true,
        inputType: 'number',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({value}) => {
        row.sendMoney = value;
        updateDetails(row).then(res => {
          this.$modal.msgSuccess("修改成功");
          this.handleView(row);
          this.getList();
        })
      })

    },
    // 查询福利发放明细
    handleView(row) {
      this.reset();
      getCldWelfareSendDetails({welfareSendId: row.welfareSendId}).then(response => {
        this.staffList = response.data;
        this.title = row.projectName + '-福利发放'
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$modal.confirm('是否确认提交？').then(() => {
            const loading = this.$loading({
              lock: true,
              text: '数据提交中...',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
            });
            addCldWelfareSend(this.form).then(res => {
              this.$modal.msgSuccess("新增成功");
              this.addOpen = false;
              this.getList();
              loading.close();
            }).catch(() => {
              loading.close();
            });
          }).catch(() => {
          });
        }
      });
    },
    // 选择项目
    handleAddProject() {
      this.isProjectOpen = true;
    },
    // 勾选项目回调
    handleMultipleProject(selection) {
      this.temporaryList = selection;
    },
    // 确认选择项目
    submitSelectedProject() {
      this.temporaryList.forEach(item => {
        this.form.projectList.push(item);
      })
      this.isProjectOpen = false;
      this.temporaryList = [];
    },
    // 取消选择项目
    cancelOpenProject() {
      this.isProjectOpen = false;
      this.temporaryList = [];
    },
    // 移除项目
    handleDeleteProject(row) {
      const index = this.form.projectList.indexOf(row);
      if (index > -1) {
        this.form.projectList.splice(index, 1);
      }
    }
  }
};
</script>
<style scoped>
.el-select {
  width: 100%;
}

.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
</style>
